package com.trdcore.zyzkt.controller;


import com.alibaba.fastjson2.JSONObject;
import com.trdcore.zyzkt.model.wechat.Menu;
import com.trdcore.zyzkt.resp.ResultData;
import com.trdcore.zyzkt.resp.ReturnCodeEnum;
import com.trdcore.zyzkt.service.MenuService;
import com.trdcore.zyzkt.utils.ConstantPropertiesUtil;
import com.trdcore.zyzkt.utils.HttpClientUtils;
import com.trdcore.zyzkt.vo.wechat.MenuVo;
import io.swagger.v3.oas.annotations.Operation;
import me.chanjar.weixin.common.error.WxErrorException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 订单明细 订单明细 前端控制器
 * </p>
 *
 * @author Luffy
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/admin/wechat/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    //获取所有菜单，按照一级和二级菜单封装
    @GetMapping("/findMenuInfo")
    public ResultData findMenuInfo() {
        List<MenuVo> list = menuService.findMenuInfo();
        return ResultData.success(list);
    }

    //获取所有一级菜单
    @GetMapping("/findOneMenuInfo")
    public ResultData findOneMenuInfo() {
        List<Menu> list = menuService.findOneMenuInfo();
        return ResultData.success(list);
    }

    @Operation(summary = "获取",description = "获取")
    @GetMapping("/get/{id}")
    public ResultData get(@PathVariable("id") Long id) {
        Menu menu = menuService.getById(id);
        return ResultData.success(menu);
    }

    @Operation(summary = "新增",description = "新增")
    @PostMapping("/save")
    public ResultData save(@RequestBody Menu menu) {
        menuService.save(menu);
        return ResultData.success(null);
    }

    @Operation(summary = "修改",description = "修改")
    @PutMapping("/update")
    public ResultData updateById(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return ResultData.success(null);
    }

    @Operation(summary = "删除",description = "删除")
    @DeleteMapping("/remove/{id}")
    public ResultData remove(@PathVariable("id") Long id) {
        menuService.removeById(id);
        return ResultData.success(null);
    }

    @Operation(summary = "根据id列表删除",description = "根据id列表删除")
    @DeleteMapping("/batchRemove")
    public ResultData batchRemove(@RequestBody List<Long> idList) {
        menuService.removeByIds(idList);
        return ResultData.success(null);
    }

    //获取access_token
    @GetMapping("/getAccessToken")
    public ResultData getAccessToken() {
            //拼接请求地址
            StringBuffer buffer = new StringBuffer();
            buffer.append("https://api.weixin.qq.com/cgi-bin/token");
            buffer.append("?grant_type=client_credential");
            buffer.append("&appid=%s");
            buffer.append("&secret=%s");
            //请求地址设置参数
            String url = String.format(buffer.toString(),
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            //发送http请求
            OkHttpClient client=new OkHttpClient();
            Request request=new Request.Builder().url(url).build();
            try(Response response=client.newCall(request).execute()) {
                String tokenString = HttpClientUtils.get(url);
//                String tokenString=response.body().string();
                System.out.println(tokenString);
                JSONObject jsonObject = JSONObject.parseObject(tokenString);
                String access_token = jsonObject.getString("access_token");
                return ResultData.success(access_token);
            }
            catch (Exception e){
                e.printStackTrace();
                return ResultData.fail(ReturnCodeEnum.RC999.getCode(),"获取access_token失败");
            }
    }


    @Operation(summary = "同步菜单",description = "同步菜单")
    @GetMapping("syncMenu")
    public ResultData createMenu() throws WxErrorException {
        menuService.syncMenu();
        return ResultData.success(null);
    }

    @Operation(summary = "删除菜单",description = "删除菜单")
    @DeleteMapping("removeMenu")
    public ResultData removeMenu() {
        menuService.removeMenu();
        return ResultData.success(null);
    }
}

